package cn.iocoder.yudao.module.blog.service.casecomment;

import cn.iocoder.yudao.module.blog.dal.dataobject.spacecomment.BlogSpaceCommentDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.casecomment.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.casecomment.BlogCaseCommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.casecomment.BlogCaseCommentConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.casecomment.BlogCaseCommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 案例评论 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogCaseCommentServiceImpl implements BlogCaseCommentService {

    @Resource
    private BlogCaseCommentMapper caseCommentMapper;

    @Override
    public Long createCaseComment(BlogCaseCommentCreateReqVO createReqVO) {
        // 插入
        BlogCaseCommentDO caseComment = BlogCaseCommentConvert.INSTANCE.convert(createReqVO);
        caseCommentMapper.insert(caseComment);
        // 返回
        return caseComment.getId();
    }

    @Override
    public void updateCaseComment(BlogCaseCommentUpdateReqVO updateReqVO) {
        // 校验存在
        validateCaseCommentExists(updateReqVO.getId());
        // 更新
        BlogCaseCommentDO updateObj = BlogCaseCommentConvert.INSTANCE.convert(updateReqVO);
        caseCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteCaseComment(Long id) {
        // 校验存在
        validateCaseCommentExists(id);
        // 删除
        caseCommentMapper.deleteById(id);
    }

    private void validateCaseCommentExists(Long id) {
        if (caseCommentMapper.selectById(id) == null) {
            throw exception(CASE_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public BlogCaseCommentDO getCaseComment(Long id) {
        return caseCommentMapper.selectById(id);
    }

    @Override
    public List<BlogCaseCommentDO> getCaseCommentList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return caseCommentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogCaseCommentDO> getCaseCommentPage(BlogCaseCommentPageReqVO pageReqVO) {
        return caseCommentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogCaseCommentDO> getCaseCommentList(BlogCaseCommentExportReqVO exportReqVO) {
        return caseCommentMapper.selectList(exportReqVO);
    }

    @Override
    public Long getChildCount(Long commentId){
        return caseCommentMapper.selectCount(BlogCaseCommentDO::getMainCommentId, commentId);
    }
}
